package top.humbleyuan.interview.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author HumbleYuan
 * @Date 2020/2/4 14:44
 * @Des
 * # ArrayList的底层扩容机制
 * # 写一个ArrayList线程不安全的例子
 */
public class ArrayListQ {

    public static void main(String[] args) {
        // ExecutorService是Executor的子接口，Executors相当于Arrays或者Collections是线程池的工具类
        ExecutorService pool = Executors.newFixedThreadPool(3);

        //List<String> as = new ArrayList<>();
        CopyOnWriteArrayList<String> as = new CopyOnWriteArrayList<>();


        try {
            for (int i = 0; i < 30; i++) {
                pool.execute(() -> {
                    as.add(UUID.randomUUID().toString().substring(0,8));
                    System.out.println(as);
                });
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }

    }

}
